/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Carolina
 */

    public class Servicios {

    public static Connection mysql() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/books", "root", "");
    }//mysqlClientes()
    
   
    public List listaTitles() throws Exception{
        
        PreparedStatement pst=mysql().prepareStatement(
               "SELECT isbn, title, editionNumber, " +
               "copyright, publisherID, imageFile, price " +
               "FROM titles ORDER BY title");
        ResultSet results=pst.executeQuery();
       
        List lista= new ArrayList();
        while(results.next()){
            BookBean book = new BookBean();

            book.setISBN( results.getString( "isbn" ) );
            book.setTitle( results.getString( "title" ) );
            book.setEditionNumber(
               results.getInt( "editionNumber" ) );
            book.setCopyright( results.getString( "copyright" ) );
            book.setPublisherID(
               results.getInt( "publisherID" ) );
            book.setImageFile( results.getString( "imageFile" ) );
            book.setPrice( results.getDouble( "price" ) );

            lista.add( book );
        }
        results.close();
        pst.close();
        return lista;
    }
    public BookBean obtenerbook(String isbn) throws Exception{
    
     PreparedStatement pst=mysql().prepareStatement(
               "SELECT isbn, title, editionNumber, " +
               "copyright, publisherID, imageFile, price " +
               "FROM titles WHERE isbn=?");
     pst.setString(1, isbn);
        ResultSet results=pst.executeQuery();
            BookBean book = new BookBean();
        while(results.next()){
            

            book.setISBN( results.getString( "isbn" ) );
            book.setTitle( results.getString( "title" ) );
            book.setEditionNumber(
               results.getInt( "editionNumber" ) );
            book.setCopyright( results.getString( "copyright" ) );
            book.setPublisherID(
               results.getInt( "publisherID" ) );
            book.setImageFile( results.getString( "imageFile" ) );
            book.setPrice( results.getDouble( "price" ) );
           
        }
        return book;
    }
}


